package com.nebula.dao.impl;

import com.nebula.dao.Database;
import com.nebula.dao.IQaDao;
import com.nebula.entity.Qa;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class QaDaoImpl extends BaseDaoImpl<Qa> implements IQaDao {

    @Override
    public List<Qa> findByIds(Integer[] ids) {
        StringBuilder tempSql = new StringBuilder("select * from qa where id in ( ");
        for (int i = 0; i < ids.length; i++) {
            tempSql.append("?,");
        }
        String sql = tempSql.toString();
        sql = sql.substring(0, sql.length() - 1) + " )";
        ResultSet rs = (ResultSet) Database.excute(sql, ids);
        List<Qa> qas = new ArrayList<>();
        try {
            if (rs != null) {
                while (rs.next()) {
                    Qa qa = new Qa();
                    qa.setId(rs.getInt("id"));
                    qa.setQuestion(rs.getString("question"));
                    qa.setCreatedTime(rs.getTimestamp("created_time"));
                    qa.setUpdatedTime(rs.getTimestamp("updated_time"));
                    qas.add(qa);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qas;
    }
}
